package pers.ervinse.shoppingmall.api;

import okhttp3.OkHttpClient;
import java.util.concurrent.TimeUnit;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://10.0.2.2:8100/";

    // 创建 OkHttpClient，设置超时
    private static OkHttpClient getOkHttpClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)  // 设置连接超时
                .writeTimeout(30, TimeUnit.SECONDS)    // 设置写入超时
                .readTimeout(30, TimeUnit.SECONDS)     // 设置读取超时
                .build();
    }

    // 创建 Retrofit 实例
    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(getOkHttpClient())  // 使用带有超时设置的 OkHttpClient
                    .build();
        }
        return retrofit;
    }

    // 获取 ApiService
    public static ApiService getApiService() {
        return getInstance().create(ApiService.class);
    }
}
